<extend name="Layout/ins_page" />
<block name="content">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Form</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <link href="/sap/demo.css" rel="stylesheet" type="text/css" />
        <script src="/sap/scripts/boot.js" type="text/javascript"></script>
    </head>
    <style>

        td{
            margin-top:10px;

        }
    </style>
    <body>
    <div id="form1" >
        <input name="id" class="mini-hidden"  value="{$data.item_id}"/>
        <table cellspacing='10' style="width: 700px; border-spacing:0px 10px;" >

            <tr>
                <td>
                    <label for="textbox1$text">大客户(VIP)供应商分类：</label>
                </td>
                <td>

                    <ul id="tree1" class="mini-tree" url="/SapSupplier/getvipsuppliercategorycode" style="width:200px;padding:5px;"
                        showTreeIcon="true" textField="text" idField="id" parentField="pid" resultAsTree="false"
                        onbeforenodeselect="onBeforeNodeSelect"
                        showRadioButton="true" showFolderCheckBox="false" expandOnLoad="false">
                    </ul>

                </td>
            </tr>

            <tr>
                <td>
                    <label for="textbox1$text">产品经理：</label>
                </td>
                <td>
                    <!--<input id="textbox1"  name="pm" class="mini-textbox" required="false" width='250px'  value ="{$data.pm}"/>-->
                    <input required="true"  id='textbox1'  name="pm" showNullItem="true" class="mini-combobox" url="/stocking/getUser?type=1" valueField="id" textField="text"   value="{$data.pm}" />
                </td>
            </tr>


            <tr>
                <td>

                </td>
                <td>

                    <br/>

                    <!--展示品类 JS 相关 Beginning-->
                    <script type="text/javascript">
                        mini.parse();
                        var tree = mini.get("tree1");
                        tree.selectNode("{$data.category_code}");
                        tree.expandPath("{$data.category_code}");

                        //--------------------------------
                        function onBeforeNodeSelect(e) {
                            var tree = e.sender;
                            var node = e.node;
                        }
                    </script>
                    <!--展示品类 JS 相关 Ending-->

                    <br/>
                    <!--<a class='mini-button' onclick="submitForm(1)" >保存</a>-->
                    <!--<a class='mini-button' onclick="" >取消</a>-->
                    <!--CloseWindow()-->


                </td>
            </tr>

        </table>
    </div>
    <script type="text/javascript">
        mini.parse();
        function submitForm(tag) {
            //提交表单数据
            var form = new mini.Form("#form1");
            form.validate();
            if (form.isValid() == false) return;

            //save begin
//            if ($(".uploader .tbody").length > 0)  {
//            }
//            else {
//                mini.alert("必须上传相关附件！");
//                return;
//            }

            //确认提示
            mini.confirm("确定修改记录？", "确定？",
                function (action) {
                    if (action == "ok") {
                        var data = form.getData();      //获取表单多个控件的数据
                        data.attachment = jQuery('#add_file_add_file').val();

                        data.brand_id = mini.get('brand-select').getValue();
                        data.brand_name = mini.get('brand-select').getText();

                        var tree = mini.get("tree1");
                        var node = tree.getSelectedNode();
                        data.category_code = node.id;

                        var json = mini.encode(data);   //序列化成JSON
                        $.ajax({
                            url: "/SapSupplier/doitemadd",
                            type: "post",
                            data: {data: json},
                            dataType: 'json',
                            success: function (data) {
                                /*if(data.code>=0){
                                     mini.confirm(data.msg+" 是否清空表单?", "清空",
                                                function (action) {
                                                    if (action == "ok") {
                                                         form.reset();
                                                    }
                                                }
                                            );

                                }else{
                                    mini.alert(data.msg);
                                }*/
                                mini.alert(data.msg);
                                //CloseWindow();
                            }
                        });

                    } else {
                    }
                });

            // save end

        }


        function CloseWindow(action) {

            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
            //location.href=document.referrer;
        }



        function onButtonEdit2(e) {
            var btnEdit = this;
            mini.open({
                url:  "/index.php?m=&c=SapSupplier&a=brandselect",
                title: "选择品牌",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                            btnEdit.setValue(data.b_id);
                            btnEdit.setText(data.b_name);
                            checkAndMergeString('');
                        }
                    }

                }
            });

        }


    </script>



    <script>
        $(function(){
            $("#group-picture1").change(function(event) {
                var img=event.target.files;
                for(var i=0;i<img.length;i++){
                    if(!img){
                        return false;
                    }
                    if(!(img[i].type.indexOf('image')==0 && img[i].type && /\.(?:jpg|png|gif)$/.test(img[i].name)) ){
                        gritter('图片只能是jpg,gif,png');
                        return false;
                    }
                    var reader = new FileReader();
                    reader.readAsDataURL(img[i]);
                    reader.onload = function(e){
                        $.post("/upload.php", {imga:e.target.result},function(ret){
                            if(ret.img!=''){

                                // check image size beginning
                                image=new Image();
                                image.src=ret.picUrl;
                                image.onload=function() {
                                    if(image.width >= 800 && image.height >= 800){

                                        console.log('upload success');
                                        var html = ' <img id="" class="group-picture" width="100px" height="100px" src="'+ret.picUrl+'" alt="物料图" >  '
                                        $('#showimg').append(html);
                                        mini.get('logo-input').setValue( mini.get('logo-input').getValue()+ret.picUrl+',');

                                    }
                                    else{
                                        alert("图片宽高必须大于等于800px");
                                        return;
                                    }
                                }
                                // check image size end

                            }else{
                                console.log('upload fail');
                            }
                        },'json');
                    }
                }
            });
        });
    </script>

    <script>
        $(function(){
            $("#group-picture2").change(function(event) {
                var img=event.target.files;
                for(var i=0;i<img.length;i++){
                    if(!img){
                        return false;
                    }
                    if(!(img[i].type.indexOf('image')==0 && img[i].type && /\.(?:jpg|png|gif)$/.test(img[i].name)) ){
                        gritter('图片只能是jpg,gif,png');
                        return false;
                    }
                    var reader = new FileReader();
                    reader.readAsDataURL(img[i]);
                    reader.onload = function(e){
                        $.post("/upload.php", {imga:e.target.result},function(ret){
                            if(ret.img!=''){

                                // check image size beginning
                                image=new Image();
                                image.src=ret.picUrl;
                                image.onload=function() {

                                    console.log('upload success');
                                    var html = ' <img id="" class="group-picture" width="100px" height="100px" src="'+ret.picUrl+'" alt="物料详情图" >  '
                                    $('#showimgB').append(html);
                                    mini.get('detail_image-input').setValue( mini.get('detail_image-input').getValue()+ret.picUrl+',');

                                }
                                // check image size end

                            }else{
                                console.log('upload fail');
                            }
                        },'json');
                    }
                }
            });
        });
    </script>

    <script type="text/javascript">

        $(document).ready(function() {
            var uploaders = {};
            $('.uploader').each(function() {
                var uploader, $target, $id;
                $target = $(this);
                $id = $target.attr('add_file_id');
                $btn_id = 'btn_' + $id;
                $uploader_id = 'uploader_' + $id;

                settings = {
                    runtimes : 'html5,flash',
                    browse_button : $btn_id, // you can pass in id...
                    container : document.getElementById($uploader_id),
                    url : upload_url,
                    flash_swf_url : app_path + '/Public/Static/plupload/Moxie.swf',
                    filters : {
                        max_file_size : '1000mb'
                    }
                };

                uploader = new plupload.Uploader(settings);
                uploaders[$id] = uploader;

                function FilesAdded(up, files) {

                    for (var i in files) {
                        var str = JSON.stringify(files);

                        if( files[i].id!=null){

                            html = '<li class="tbody" id="' + files[i].id + '">\n';

                            html += '<div class="loading"></div>\n';
                            html += '<div class="data">\n';

                            html += '<span class="del text-center"><a class="link del">删除</a></span>\n';

                            html += '<span class="size text-right">' + plupload.formatSize(files[i].size) + '</span>';
                            html += '<span class="auto autocut">' + files[i].name + '</span>';
                            html += '</li>';
                            html += '</div>\n';

                            $('#uploader_' + $id + ' .file_list').append(html);
                        }
                    }
                    up.start();
                }

                uploader.bind("PostInit", function(up) {
                    if ($(".uploader .tbody").length > 0) {
                        $(".uploader .tbody .loading").css("width", "100%");
                        $(".uploader .thead").show();
                        $(".uploader .tbody").each(function() {
                            id = $(this).attr("filename");
                            filename = $(this).attr("filename");
                            size = $(this).attr("size");
                            file = new plupload.File(id, filename, size);
                            file.status = plupload.DONE;
                            count = uploader.files.length;
                            uploader.files[count] = file;
                            path = 'hhhhhhh';
                        });
                    }
                });

                uploader.init();

                uploader.bind('FilesAdded', FilesAdded);

                uploader.bind("UploadProgress", function(up, file) {
                    $("#" + file.id).find("a.del").hide();
                    $("#" + file.id).find('.loading').css("width", file.percent + "%");
                });

                uploader.bind('FileUploaded', function(up, file, data) {
                    var myObject = eval('(' + data.response + ')');
                    if (myObject.status) {
                        if ($("#add_file_" + $id).length != 0) {
                            $("#add_file_" + $id).val($("#add_file_" + $id).val() + myObject.sid + ";");
                        }
                        $("#" + file.id).attr("add_file", myObject.sid);

                        $new_upload = $("#uploader_" + $id + " .file_list").attr("new_upload");
                        $("#uploader_" + $id + " .file_list").attr("new_upload", $new_upload + myObject.sid + ";");

                        $("#" + file.id).find("a.del").show();
                    } else {
                        ui_alert(myObject.info, function() {
                            $("#" + file.id).remove();
                        });
                    }
                });
            });
        });

        window.onbeforeunload = function(e) {
            e = e || window.event;
            // For IE and Firefox prior to version 4
            $new_upload = $(".file_list").attr("new_upload");
            if ($new_upload !== undefined) {
                if ($new_upload.length) {
                    if (e) {
                        e.returnValue = '上传的附件将被删除，确定退出吗？';
                    }
                    // For Safari
                    return '上传的附件将被删除，确定退出吗？';
                }
            }
        };

        $(document).on("click", ".uploader a.del", function() {
            $obj = $(this).parents("li");
            $uploader = $(this).parents('.uploader');
            ui_confirm("确定要删除吗？", function() {

                $current_del_file = $obj.attr("add_file");
                $(".add_file", $uploader).val($(".add_file", $uploader).val().replace($current_del_file + ";", ""));

                $new_upload = $(".file_list", $uploader).attr("new_upload");
                $(".file_list", $uploader).attr("new_upload", $new_upload.replace($current_del_file + ";", ""));

                $obj.remove();
            });
        });
    </script>

    <script type="text/javascript">

        function checkAndMergeString(item) {

            //alert(item.value);
            //data.brand_id = mini.get('brand-select').getValue();
            //data.brand_name = mini.get('brand-select').getText();

            var shortName = mini.get('item_short_name').getValue();
            var brand = mini.get('brand-select').getText();
            var model = mini.get('model').getValue();
            var specifications = mini.get('specifications').getValue();
            var color = mini.get('color').getValue();
            var mergedString = brand+' '+shortName+' '+model+' '+specifications+' '+color;
            mini.get('item_name').setValue(mergedString);
            //alert(mergedString);

        }

    </script>

    </body>
    </html>

</block>
<block name="js">
    <script type="text/javascript">
        function save() {
            sendForm("form_data", "{:U('save')}", "{:U('index')}");
        }
    </script>
</block>